package com.cgy.service.Impl;

import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cgy.entity.Computer;
import com.cgy.repository.ComputerRepository;
import com.cgy.service.ComputerService;
@Service
public class computerServiceImpl implements ComputerService {

@Autowired
private ComputerRepository computerRepository;
@Autowired
private ComputerService comService;
	@Override
	public Page<Computer> findBynameConputer(Computer c, Pageable pageable,Integer zid) {
		// TODO Auto-generated method stub
		return computerRepository.findAll(get(c,zid), pageable);
	}

	private Specification<Computer> get(Computer c,Integer zid) {
		// TODO Auto-generated method stub
		return new Specification<Computer>() {

			@Override
			public Predicate toPredicate(Root<Computer> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				//创建动态sql
				Predicate predicate = criteriaBuilder.conjunction();
				List<Expression<Boolean>> exList = predicate.getExpressions();
				if(c.getCstatus()!=null) {
					exList.add(criteriaBuilder.equal(root.get("cstatus"),c.getCstatus()));
				}
				
				
				  if(zid!=null) {
                         exList.add(criteriaBuilder.equal(root.get("ziYuan").get("zid"), zid)) ;
				  } 
				  
				 
				/*
				 * if(!StringUtils.isEmpty(c.getZiYuan().getZid())) {
				 * exList.add(criteriaBuilder.like(root.get("ziYuan").get("zid"),"%"+c.getZiYuan
				 * ().getZid()+"%")); }
				 */
			
				return predicate;
			}
		};
	}
	@Transactional
	@Override
	public Computer addAndUpdateConputer(Computer c) {
		// TODO Auto-generated method stub
		return computerRepository.saveAndFlush(c);
	}
	@Transactional
	@Override
	public Integer deleteConputer(Integer cid) {
		// TODO Auto-generated method stub
		return computerRepository.deleteComputer(cid);
	}

	@Override
	public Integer upcomputerstaticsj(Integer cid) {
		// TODO Auto-generated method stub
		return computerRepository.upcomputerstaticsj(cid);
	}

	@Override
	public Integer upcomputerstaticxj(Integer cid) {
		// TODO Auto-generated method stub
		return computerRepository.upcomputerstaticxj(cid);
	}
	//根据资源室id查询资源

	@Override
	public List<Computer> findAllByzid(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return computerRepository.findAllByzid(map);
	}
	//总条数

	@Override
	public Integer findAllByzidCount(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return computerRepository.findAllByzidCount(map);
	}


	

	



	
	
}
